/*Copyright (c) 2007 Adobe Systems Incorporated

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
/**
 * HDashedRule
 * 
 * This component extends HRule and replaces its shadowed line with a dashed (or dotted) line.
 * 
 * This component uses HRule's strokeColor and strokeWidth (to define the size of the dash) and
 * adds the style dashGap (to indicate the spacing between stokes).
 */
package com.commods.main.view.components.historicalChart.proto.temp
{
	import flash.display.Graphics;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.controls.HRule;
	import mx.controls.Label;
	import mx.controls.VRule;
	import mx.graphics.IStroke;
	import mx.graphics.Stroke;

	/**
	 * dashGap
	 * 
	 * This is the spacing between the dashes as defined by the strokeWidth. The default is
	 * 2xstrokeWidth.
	 */
	[Style(name="dashGap",type="Number",inherit="no")]
	
	public class VDashRule extends VRule
	{
		private var _lbl:Label;
		
		public function VDashRule()
		{
			super();
			
		}
		
		//{region CLASS_INTERFACE
		private var _bottomLabelText:String, __bottomLabelTextDirty:Boolean;
		public function get bottomLabelText():String
		{
			return _bottomLabelText;
		}
		
		public function set bottomLabelText(value:String):void
		{
			_bottomLabelText = value;
			__bottomLabelTextDirty=true;
			
			invalidateProperties();
			invalidateDisplayList();
		}
		//}endregion
		
		//{region LC
		override protected function createChildren():void{
			super.createChildren();
			_lbl = new Label();
			_lbl.width=Number(undefined);
			_lbl.height=Number(undefined);
			addChild(_lbl);
		}
		
		override protected function commitProperties():void{
			super.commitProperties();
			if(__bottomLabelTextDirty){
				_lbl.text = bottomLabelText;
				__bottomLabelTextDirty=false;
			}
		}
		
		/*
		 * Draws the dashed line.
		 */
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			/*
			 * do not call super.updateDisplayList - we want to completely override
			 * the drawing of the rule
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			 */
			 			
			var strokeColor:Number = getStyle("strokeColor");
			var strokeWidth:Number = getStyle("strokeWidth");
			var dashGap:Number = getStyle("dashGap");
			if( isNaN(dashGap) ) dashGap = 2*strokeWidth;
			// create the pattern. The drawDashedLine function can draw more complex patterns, but for
			// a simple dash, you need just 2 elements.
			var pattern:Array = [strokeWidth, dashGap];
			
			graphics.clear();
			graphics.moveTo(0,0);
			var struct:DashStruct = new DashStruct();							
			_drawDashedLine(graphics,new Stroke(strokeColor,strokeWidth,1),pattern,struct,0,0,0,unscaledHeight);
	
			_lbl.move(-_lbl.measuredWidth/2, unscaledHeight);
		}
		//}endregion
		
		/**
		 * drawDashedLine
		 * 
		 * This function is from Ely Greenfield's Quietly Scheming blog where it was used to
		 * draw patterned lines on charts.
		 * 
		 * (www.quietlyscheming.com)
		 */
		protected static function _drawDashedLine(target:Graphics,stroke:IStroke,pattern:Array,
												drawingState:DashStruct,
												x0:Number,y0:Number,x1:Number,y1:Number):void
		{			
			var dX:Number = x1 - x0;
			var dY:Number = y1 - y0;
			var len:Number = Math.sqrt(dX*dX + dY*dY);
			dX /= len;
			dY /= len;
			var tMax:Number = len;
			
			
			var t:Number = -drawingState.offset;
			var bDrawing:Boolean = drawingState.drawing;
			var patternIndex:int = drawingState.patternIndex;
			var styleInited:Boolean = drawingState.styleInited;
			while(t < tMax)
			{
				t += pattern[patternIndex];
				if(t < 0)
				{
					var x:int = 5;
				}
				if(t >= tMax)
				{
					drawingState.offset = pattern[patternIndex] - (t - tMax);
					drawingState.patternIndex = patternIndex;
					drawingState.drawing = bDrawing;
					drawingState.styleInited = true;
					t = tMax;
				}
				
				if(styleInited == false)
				{
					if(bDrawing)
						stroke.apply(target, new Rectangle(), new Point(0, 0));
					else
						target.lineStyle(0,0,0);
				}
				else
				{
					styleInited = false;
				}
					
				target.lineTo(x0 + t*dX,y0 + t*dY);

				bDrawing = !bDrawing;
				patternIndex = (patternIndex + 1) % pattern.length;
			}
		}
	}
}

class DashStruct
{
	public function init():void
	{
		drawing = true;
		patternIndex = 0;
		offset = 0;
	}
	public var drawing:Boolean = true;
	public var patternIndex:int = 0;
	public var offset:Number = 0;	
	public var styleInited:Boolean = false;
}